package com.tencent.iov.cowin.befsale.clue.repository.tenant.impl;

import com.tencent.iov.cowin.befsale.clue.dao.http.tenant.TenantClient;
import com.tencent.iov.cowin.befsale.clue.dao.http.tenant.request.QueryDictionariesRequest;
import com.tencent.iov.cowin.befsale.clue.dao.http.tenant.response.DictionariesResponse;
import com.tencent.iov.cowin.befsale.clue.repository.tenant.TenantManager;
import com.software.boot.common.entity.Response;
import com.software.boot.common.util.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author xiamu
 * @Date 2022/1/29 10:58
 * @Version 1.0
 **/
@Service
public class TenantManagerImpl implements TenantManager {
    @Autowired
    private TenantClient tenantClient;

    @Override
    public Map<String, String> getDicMapGroupByCode(QueryDictionariesRequest request) {
        Response<List<DictionariesResponse>> dicResponse = tenantClient.getDictionariesListByParam(request);
        if (EmptyUtils.isEmpty(dicResponse) || EmptyUtils.isEmpty(dicResponse.getData())) {
            return null;
        }
        return dicResponse.getData().stream().collect(Collectors.toMap(DictionariesResponse::getCode,
                DictionariesResponse::getName));
    }

    @Override
    public List<DictionariesResponse> getDicListByParam(QueryDictionariesRequest request) {
        Response<List<DictionariesResponse>> dicResponse = tenantClient.getDictionariesListByParam(request);
        if (!Response.CODE_SUCCESS.equals(dicResponse.getCode()) || EmptyUtils.isEmpty(dicResponse.getData())) {
            return null;
        }
        return dicResponse.getData();
    }

    @Override
    public List<DictionariesResponse> getListByParentCode(String parentCode) {
        Response<List<DictionariesResponse>> response = tenantClient.getListByParentCode(parentCode);
        if (!Response.CODE_SUCCESS.equals(response.getCode()) || EmptyUtils.isEmpty(response.getData())) {
            return null;
        }
        return response.getData();
    }
}
